<?php
define('GAME', 		'_z');
define('USE_LOCAL', isset($_REQUEST['uselocal']) && '1' == $_REQUEST['uselocal'] ? true : false);

define('ROOT', 		dirname(__FILE__) . '/');

define('CFG_PATH_FLASH', 		'path_flash');
define('CFG_PATH_IMAGE', 		'path_image');
define('CFG_PATH_GAME', 		'path_game');
define('CFG_PATH_GAME_BASIC', 	'path_game_basic');
define('CFG_PATH_SECURITY',		'path_security');
define('CFG_PATH_GATEWAY',		'path_gateway');
define('CFG_PATH_XML_SYSTEMS',	'path_xml_systems');
define('CFG_PATH_XML_ROOMS',	'path_xml_rooms');
define('CFG_PRIVATE_KEY',		'private_key');
define('CFG_FUNC_SWITCH', 		'func_switch');
define('CFG_IS_ADMIN', 			'is_admin');

include_once ROOT . '../ujn2/ujn.php';

ujn_runtime::setSplitAlias('lilith');

function start_crontab() {
	$cfg = require_once ROOT . '/configs/config.php';
	
	$cfg['controllers_path']			= ROOT . '/crontab/';
	$cfg['default_controllers_name']	= 'auto';
	ujn::runApplication( $cfg );
}

function main(ujn_controller $c) { // 游戏主頁面

	$mid = isset($_POST['uid']) ? $_POST['uid'] : 0;
	if(empty($mid)) { /* 使用js方式跳转 */
		$c->redirect(ujn::getCFG(CFG_PATH_GAME), 'js');
	}
	$_url		= isset($_POST['url'])		? $_POST['url'] : '';
	$_user		= isset($_POST['user'])		? $_POST['user'] : '';
	$_friends	= isset($_POST['friends'])	? $_POST['friends'] : '';
	$_time		= isset($_POST['time'])		? $_POST['time'] : '';
	$_sig		= isset($_POST['sig'])		? $_POST['sig'] : '';

	if($_sig != md5($_url . $mid . $_time . ujn::getCFG(CFG_PRIVATE_KEY) . $_friends . $_user)) die('unaccess, u need refresh!');

	$_url		= urldecode($_url);
	$_user		= urldecode($_user);
	$_friends	= urldecode($_friends);

	// get user info in game;
	$appUser = mdl_member_info::getInstance()->getMemberByUID4Flash($mid);

	// make flash var
	$fv = __build_flash_vars($mid, $appUser);

	// frist signin today
	$is_firstin = 0;
	if( date( 'Ymd', (int)$appUser['mactivetime'] - 25200 ) != date( 'Ymd', NOW - 25200 ) ) {
		mdl_member_info::getInstance()->incSignIned($mid, $appUser);  // 更新用户登录次数 & 最后登录时间
		$is_firstin = 1;
	}
	
	$c->assign(array(
		'app_swf_path'	=> $fv['urls']['flashurl'] . "App{$fv['flashver']['App']}.swf?" . NOW,

		'fv'					=> $fv,
		'is_firstin'			=> $is_firstin,
		'is_register'			=> (isset($appUser['is_register']) && '1' == $appUser['is_register']) ? '1' : '0',

		//'user_winlog_histroy' 	=> $user_winlog_history,

		// json format, use it only javascript
		'p_user'				=> $_user,
		'g_user'				=> $appUser,
		'friends'				=> $_friends,

		'game_url'				=> ujn::getCFG(CFG_PATH_GAME),
		'game_basic_url'		=> ujn::getCFG(CFG_PATH_GAME_BASIC),
		'img_url'				=> ujn::getCFG(CFG_PATH_IMAGE),
	));
	
	return $c;
}


function __build_flash_vars($mid, $fv) {

	if( $mid != tls_func::uint($mid) || !is_array($fv) ) return array();

	// make flash var
	list( $fv['mtkey'], $fv['tid'], $fv['mtstatus'], $fv['svid'] ) = mdl_member_online::getInstance()->insert($mid);

	$fv['mid'] = $mid;

	$fv['gateway']	= ujn::getCFG(CFG_PATH_GATEWAY);
	$fv['xmlurls']	= array(
		'system' => ujn::getCFG(CFG_PATH_XML_SYSTEMS),
		'rooms' => ujn::getCFG(CFG_PATH_XML_ROOMS),
	);

	$fversion	= isset($_REQUEST['fversion']) ? $_REQUEST['fversion'] : '';
	if(empty($fversion)) {
		$cfg_flashver_static_file = ujn::getCFG('tmp_path') . "/cfg/v.php";
		$fversion = is_file($cfg_flashver_static_file) ? trim(file_get_contents($cfg_flashver_static_file)) : '0.1';
	}
	$fv['version']	= $fversion;
	// 特殊情况
	$fv['flashver']	= require_once ujn::getCFG('tmp_path') . '/cfg/' . ( USE_LOCAL ? "v.local.php" : "v.{$fversion}.php" );

	$fv['hall']	= ujn::getCFG('servers.hall');
	$fv['urls']		= array(
			'flashurl'	=> USE_LOCAL ? ujn::getCFG(CFG_PATH_FLASH) : ujn::getCFG(CFG_PATH_FLASH) . $fversion . '/',
	);
	$fv['v']		= microtime(true);
	$fv['adtime']	= NOW;

	return $fv;
}